package com.gaber.generics;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gaber
 * @date 2023/7/14 15:20:32
 * @description TODO
 */
public class GenericMethodTest {
    //遍历数据
    public static <E> int printArray(E[] inputArray) {
        for (E element: inputArray
             ) {
            System.out.printf("%S ", element);
        }
        System.out.println();
        return 1;
    }

    //计算最大值
    public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
        T max = x;
        if(y.compareTo(max) > 0) {
            max = y;
        }
        if(z.compareTo(max) > 0) {
            max = z;
        }
        return max;
    }

    public static class Box<T> {
        private T t;
        public void add(T t) {
            this.t = t;
        }
        public T get() {
            return t;
        }
    }

    public static void getData(List<?> data) {
        System.out.println("data: "+data.get(0));
    }

    public static void getUperNumber(List<? extends Number> data) {
        System.out.println("data: "+ data.get(0));
    }

    public static void main(String[] args) {
        //
        Integer[] intArray = {1,2,3,4,5};

        Double[] doubleArray = {1.1,2.3,5.6,7.7};

        String[] charArray = {"H","E", "L","L", "O"};

        System.out.println( "整型数组元素为:" );
        printArray( intArray  ); // 传递一个整型数组

        System.out.println( "双精度数组元素为:" );
        printArray(doubleArray);

        System.out.println( "字符数组元素为:" );
        printArray(charArray);

        System.out.printf( "%d, %d 和 %d 中最大的数为 %d\n\n",
                3, 4, 5, maximum( 3, 4, 5 ) );

        System.out.printf( "%.1f, %.1f 和 %.1f 中最大的数为 %.1f\n\n",
                6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );

        System.out.printf( "%s, %s 和 %s 中最大的数为 %s\n","pear",
                "apple", "orange", maximum( "pear", "apple", "orange" ) );

        Box<Integer> integerBox = new Box<Integer>();
        integerBox.add(new Integer(10));
        System.out.printf("整数类型为：%d\n\n", integerBox.get());

        Box<String> stringBox = new Box<String>();
        stringBox.add(new String("菜鸟教程"));
        System.out.println("字符串为" + stringBox.get());

        List<String> name = new ArrayList<>();
        List<Integer> age = new ArrayList<>();
        List<Number> number = new ArrayList<>();

        name.add("icon");
        age.add(18);
        number.add(314);

        getData(name);
        getData(age);
        getData(number);

//        getUperNumber(name);
        getUperNumber(age);
        getUperNumber(number);


    }
}
